package com.example.coursaccino;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Calendar;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

public class CreateCourseActivity extends Activity {
	
	private TextView DisplayTime1;
	private TextView DisplayTime2;	
	private TextView dayText;
	
	private TimePicker timePicker1;
	private Button btnChangeTime;
 
	private int hour;
	private int minute;
	
	static final int TIME_DIALOG_ID1 = 999;
	static final int TIME_DIALOG_ID2 = 998;	

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_course);        
        
        ((Button) findViewById(R.id.day_button)).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				this.MultipleChoiceAlertBox();
			}

			private void MultipleChoiceAlertBox() {
				// TODO Auto-generated method stub
				final ArrayList<Integer> selectVal = new ArrayList();
				Resources r = getResources();
	        	final String[] days = r.getStringArray(R.array.days); 
				
				Builder builder = new AlertDialog.Builder(CreateCourseActivity.this);  
		        builder.setTitle(R.string.choose_date);  
		        builder.setMultiChoiceItems(R.array.days, null,  
		            new DialogInterface.OnMultiChoiceClickListener() {  
		              @Override  
		              public void onClick(DialogInterface dialog, int which,  
		                  boolean isChecked) {  
		            	  if(isChecked)
		            		  selectVal.add(which);
		            	  else if (selectVal.contains(which)) {
		                       // Else, if the item is already in the array, remove it 
		                       selectVal.remove(Integer.valueOf(which));
		                   }
		              }  
		            });  
		        builder.setPositiveButton("Set Days", new DialogInterface.OnClickListener() {  
		          @Override  
		          public void onClick(DialogInterface dialog, int which) {		        	 
		        	String selectedVal = "";
		            for (int i = 0; i < days.length; i++) {  
		              if (selectVal.contains(i)) {  
		                selectedVal += days[i] + ", ";  
		              }  
		            }
		            int len = selectedVal.length();
		            selectedVal = selectedVal.substring(0, len-2); //remove the trailing comma and space
		            dayText = (TextView) findViewById(R.id.day_text);
		            dayText.setText(selectedVal);
		          }  
		        });  
		        builder.setNegativeButton("Cancel",  
		            new DialogInterface.OnClickListener() {  
		              @Override  
		              public void onClick(DialogInterface dialog, int which) {  
		                dialog.dismiss();  
		              }  
		            });  
		        AlertDialog alert = builder.create();  
		        alert.show();
			}
		});
        
        this.setCurrentTimeOnView();
        this.getFromTime();     
        this.getToTime();
    }
    
    public void setCurrentTimeOnView() {
    	 
		DisplayTime1= (TextView) findViewById(R.id.from_time_text);
		DisplayTime2 = (TextView) findViewById(R.id.to_time_text);
		
		final Calendar c = Calendar.getInstance();
		hour = c.get(Calendar.HOUR_OF_DAY);
		minute = c.get(Calendar.MINUTE);
 
		// set current time into textview
		DisplayTime1.setText(
                    new StringBuilder().append(pad(hour))
                                       .append(":").append(pad(minute)));
		DisplayTime2.setText(
                new StringBuilder().append(pad(hour))
                                   .append(":").append(pad(minute)));	
	}
    
    public void getFromTime() {
    	btnChangeTime = (Button) findViewById(R.id.from_time_button);    	 
		btnChangeTime.setOnClickListener(new View.OnClickListener() {
 
			@Override
			public void onClick(View v) { 
				showDialog(TIME_DIALOG_ID1); 
			} 
		});
    }
    
    public void getToTime() {
    	btnChangeTime = (Button) findViewById(R.id.to_time_button);    	 
		btnChangeTime.setOnClickListener(new View.OnClickListener() {
 
			@Override
			public void onClick(View v) { 
				showDialog(TIME_DIALOG_ID2); 
			} 
		});
    }
    
    @Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case TIME_DIALOG_ID1:
			// set time picker as current time
			return new TimePickerDialog(this, timePickerListener1, hour, minute,true); 
		case TIME_DIALOG_ID2:
			// set time picker as current time
			return new TimePickerDialog(this, timePickerListener2, hour, minute,true);	
		}
		return null;
	}
 
	private TimePickerDialog.OnTimeSetListener timePickerListener1 = 
            new TimePickerDialog.OnTimeSetListener() {
		public void onTimeSet(TimePicker view, int selectedHour,
				int selectedMinute) {
			
			hour = selectedHour;
			minute = selectedMinute;
			
			// set current time into textview
			DisplayTime1.setText(new StringBuilder().append(pad(hour))
					.append(":").append(pad(minute)));					
		}
	};
	
	private TimePickerDialog.OnTimeSetListener timePickerListener2 = 
            new TimePickerDialog.OnTimeSetListener() {
		public void onTimeSet(TimePicker view, int selectedHour,
				int selectedMinute) {
			
			hour = selectedHour;
			minute = selectedMinute;
			
			// set current time into textview
			DisplayTime2.setText(new StringBuilder().append(pad(hour))
					.append(":").append(pad(minute)));					
		}
	};
 
	private static String pad(int c) {
		if (c >= 10)
		   return String.valueOf(c);
		else
		   return "0" + String.valueOf(c);
	}
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_create_course, menu);
        return true;
    }
}